<template>
  <div class="search">
    <f-form ref="form" :action="api.busOrder" :form-key="this.formItem.orderId">

      <f-form-item label="部门" name="deptId" type="select" :show-search='true' :data="api.dept.list" valueKey="deptId" :value="formItem.deptId" :required="true" label-key="deptName" @change="deptChange"/>

      <a-descriptions style="padding-left: 130px" v-if="loadDept">
        <a-descriptions-item label="业务员">{{formItem.saleManName}}</a-descriptions-item>
        <a-descriptions-item label="资信天数">{{formItem.reputation}}</a-descriptions-item>
        <a-descriptions-item label="资信额度">{{formItem.reputationAmount}}</a-descriptions-item>
      </a-descriptions>
      <f-form-item label="购货方" placeholder="请选择购货方"  name="customName" :readOnly="true" :required="true" :value.sync="formItem.customName" :trigger-change="true">
        <a-tooltip slot="addonBefore" placement="top" title="点击图标选择购货方">
          <a-icon type="user" style="cursor:pointer" @click="openCustomModal"/>
        </a-tooltip>
      </f-form-item>
      <f-form-item label="供货方" placeholder="请选择供货方" name="supplierName" :required="true" :readOnly="true" :disabled="!supplieBtn" :value.sync="formItem.supplierName" :trigger-change="true">
        <a-tooltip slot="addonBefore" placement="top" title="点击图标选择供货方" v-if="supplieBtn">
          <a-icon type="user" style="cursor:pointer" @click="openSupplierModal"/>
        </a-tooltip>
        <a-tooltip slot="suffix" placement="topRight" title="删除供货方" v-if="supplieBtn">
          <a-icon type="delete" @click="deleteSupplier"/>
        </a-tooltip>
        <a-tooltip slot="addonBefore" placement="top" title="温馨提示:当订单添加商品后,供货商禁止修改,如需修改请删除订单商品" v-if="!supplieBtn">
          <a-icon type="warning"/>
        </a-tooltip>
      </f-form-item>
      <f-form-item label="购方资信方式" type="radio" name="customBondType" :required="true" dataType="number" :data="api.busOrder.customBondType" :value.sync="formItem.customBondType" @change="customBondTypeChange"/>
      <f-form-item label="公司垫付方式" type="radio" name="supplierBondType" :required="true" dataType="number" :data="api.busOrder.supplierBondType" :value.sync="formItem.supplierBondType"/>
      <f-form-item :label="amountTitle" type="date" name="takeDate" :required="true" :value="formItem.takeDate" v-if="formItem.customBondType!=5"/>
      <f-form-item label="收款金额" type="number" name="takeAmount" dataType="number" :value="formItem.takeAmount" :precision="4" :required="true" v-if="formItem.customBondType!=0&&formItem.customBondType!=5"/>
      <f-form-item label="收款账户" ref="bank" type="select" name="bankId" :value="formItem.bankId" :data="api.bank.list" valueKey="bankId" label-key="bankName" :required="true" v-if="formItem.customBondType!=0&&formItem.customBondType!=5" :autoLoad="false"/>
      <f-form-item label="发票方式" name="invoiceSendType" v-if="loaded" type="radio" dataType="number"  :data="api.busOrder.invoiceSendType" :required="true" :value.sync="formItem.invoiceSendType"/>
      <f-form-item label="发货日期" :required="true" type="date" name="sendDate" :value="formItem.sendDate"/>
      <f-form-item label="收货地址" type="select" name="customShId"  :value="formItem.customShId" :data="api.customAddress.list" valueKey="customAddressId" label-key="userName,address,telephone" :params="params1" :dropdown-render="true" @addItem="addItem"/>
      <f-form-item label="备注" name="remark" :value="formItem.remark"/>
    </f-form>
    <select-custom-modal ref="customModal" @onSelectChange="onSelectCustomChange"></select-custom-modal>
    <select-supplier-modal ref="supplierModal" @onSelectChange="onSelectSupplierChange"></select-supplier-modal>
    <custom-address-modal ref="addressModal" :addressType="addressType" :custom-id="this.params1.customId" @handleSuccess="handleSuccess"></custom-address-modal>
  </div>
</template>
<script>
  import api from './js/api'
  import SelectCustomModal from '../custom/select_custom_modal'
  import SelectSupplierModal from '../supplier/select_supplier_modal'
  import CustomAddressModal from '../custom/custom_address_modal'

  export default {
    name: 'orderForm',
    props: {},
    data() {
      return {
        refs: this.$refs,
        addressType: 1,
        params1: {
          customId: '',
          addressType: 1
        },
        customShData: {},
        loadDept: false,
        supplieBtn: true,
        amountTitle: '回款日期',
        api: Object.assign(api, this.api),
        isAdd: true,//是否是添加模式
        loaded: false,//是否加载完成
        formItem: this.getFormItem()
      }
    },
    watch: {
      'formItem.customBondType': function(newVal) {
        if (newVal == 0) {
          this.amountTitle = '回款日期'
        } else {
          this.amountTitle = '收款日期'
          this.$nextTick(() => {
            this.$refs['bank'].initData()
          })
        }
      }
    },
    methods: {
      getFormItem() {
        return {
          orderId: '',
          orderNo: '',
          delivery: '',
          saleManName: '',
          customName: '',
          invoiceSendType: 0,
          reputation: '',
          reputationAmount: '',
          customShId:null,
          bankId: '',
          customBondType: 0,
          supplierBondType: 0,
          customId: '',
          billsType: '',
          remark: ''
        }
      },
      //表单赋值
      setData(data) {
        this.$refs.form.resetFields()
        this.formItem = this.getFormItem()
        if (data && data.orderId) {
          let params = {
            orderId: data.orderId
          }
          this.refs['form'].getDetails(params, (data) => {
            this.formItem = data
            this.loaded = true
            if (data.saleAmount) {
              this.supplieBtn = false
            } else {
              this.supplieBtn = true
            }
          })
          this.isAdd = false
        } else {
          this.loaded = true
        }
      },
      //清空供货方
      deleteSupplier() {
        this.$refs.form.getForm().setFieldsValue({ 'supplierName': '' })
        this.formItem.supplierId = ''
      },
      customBondTypeChange(v) {

      },
      addItem(data) {
        this.customShData = data
        this.addressType = 1
        this.$refs['addressModal'].showModal()
      },
      //地址自定义添加回调
      handleSuccess(data) {
        if (this.addressType == 1) {
          this.customShData.push(data)
        }
      },
      openCustomModal() {
        this.$refs['customModal'].open()
      },
      openSupplierModal() {
        this.$refs['supplierModal'].open()
      },
      onSelectCustomChange(selectedRowKeys, selectedRows) {
        this.$refs.form.getForm().setFieldsValue({ 'customName': selectedRows[0].customName })
        this.formItem.customName = selectedRows[0].customName
        this.formItem.customId = selectedRowKeys[0]
      },
      onSelectSupplierChange(selectedRowKeys, selectedRows) {
        this.$refs.form.getForm().setFieldsValue({ 'supplierName': selectedRows[0].supplierName })
        this.formItem.supplierName = selectedRows[0].supplierName
        this.formItem.supplierId = selectedRowKeys[0]
        this.formItem.supplierBondType = selectedRows[0].supplierBondType
      },
      //提交表单
      handleSubmit(callback) {
        this.$refs.form.submit({ customId: this.formItem.customId, supplierId: this.formItem.supplierId,overAmount:this.formItem.overAmount,processInstanceId:this.formItem.processInstanceId }, callback)
      },
      deptChange(value) {
        this.http.get(this, {
          url: this.api.dept.get,
          params: {
            deptId: value,
            handlerName: 'deptGetHandler'
          },
          success: function(me, data) {
            me.loadDept = true
            me.formItem.saleManName = data.saleManName
            me.formItem.reputation = data.reputation
            me.formItem.reputationAmount = data.reputationAmount
          }
        })
      }
    },
    mounted() {

    },
    created() {
    },
    components: { SelectSupplierModal, SelectCustomModal,CustomAddressModal }
  }
</script>

<style scoped lang="less" rel="stylesheet/less">

</style>
